package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.common.util.Cons;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * cookie的工具类
 */
public class CookieUtil {
    public static void addCookie(HttpServletResponse response, String name, String value) {
        //将cookie信息返回给浏览器，同时给redis缓存中的cookie存入
        Cookie cookie = new Cookie(name, value);
        //可在同一应用服务器内共享cookie
        cookie.setPath("/");
        //设置cookie在浏览器中最大存活时间
        cookie.setMaxAge(Cons.TOKEN_TIME*60);
        //使用reaponse返回给浏览器
        response.addCookie(cookie);


    }

    public static Cookie getCookie(HttpServletRequest request, String CookieName) {
        //通过request对象获取
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for(Cookie cookie:cookies){
                if(cookie.getName().equals(CookieName) ){
                    return cookie;
                }
            }
        }
        return null;
    }
}
